#!/bin/bash

if [ ! -f util/.serverpod_util_root ]; then
    echo "Run this script from the root of the Serverpod repository"
    echo "I.e. util/run_tests_analyze"
    exit 1
fi


LOG_LEVEL="--fatal-infos"
for arg in "$@"
do
    # Check if the argument is the --allow-infos flag
    # If it is, set the log level to --fatal-warnings instead of --fatal-infos
    if [ "$arg" == "--allow-infos" ] ; then
        LOG_LEVEL="--fatal-warnings"
        break
    elif [ "$arg" == "--allow-warnings" ] ; then
        LOG_LEVEL="--no-fatal-warnings"
        break
    fi
done

declare -a projectPaths=(
    "packages/serverpod"
    "packages/serverpod_client"
    "packages/serverpod_serialization"
    "packages/serverpod_service_client"
    "packages/serverpod_shared"
    "packages/serverpod_flutter"
    "packages/serverpod_test"

    "examples/legacy/chat/chat_server"
    "examples/legacy/chat/chat_client"
    "examples/legacy/chat/chat_flutter"

    "tests/serverpod_test_server"
    "tests/serverpod_test_client"
    "tests/serverpod_test_flutter"
    "tests/serverpod_test_module/serverpod_test_module_server"
    "tests/serverpod_test_module/serverpod_test_module_client"
    "tests/serverpod_test_nonvector/serverpod_test_nonvector_server"
    "tests/serverpod_test_nonvector/serverpod_test_nonvector_client"
    "tests/serverpod_new_auth_test/serverpod_new_auth_test_server"
    "tests/serverpod_new_auth_test/serverpod_new_auth_test_client"

    "modules/legacy/serverpod_auth/serverpod_auth_server"
    "modules/legacy/serverpod_auth/serverpod_auth_client"
    "modules/legacy/serverpod_auth/serverpod_auth_shared_flutter"
    "modules/legacy/serverpod_auth/serverpod_auth_apple_flutter"
    "modules/legacy/serverpod_auth/serverpod_auth_google_flutter"
    "modules/legacy/serverpod_auth/serverpod_auth_email_flutter"
    "modules/legacy/serverpod_auth/serverpod_auth_firebase_flutter"

    "modules/new_serverpod_auth/serverpod_auth_core/serverpod_auth_core_flutter"
    "modules/new_serverpod_auth/serverpod_auth_core/serverpod_auth_core_server"
    "modules/new_serverpod_auth/serverpod_auth_core/serverpod_auth_core_client"
    "modules/new_serverpod_auth/serverpod_auth_migration/serverpod_auth_migration_server"
    "modules/new_serverpod_auth/serverpod_auth_migration/serverpod_auth_migration_client"
    "modules/new_serverpod_auth/serverpod_auth_idp/serverpod_auth_idp_server"
    "modules/new_serverpod_auth/serverpod_auth_idp/serverpod_auth_idp_client"
    "modules/new_serverpod_auth/serverpod_auth_bridge/serverpod_auth_bridge_server"
    "modules/new_serverpod_auth/serverpod_auth_bridge/serverpod_auth_bridge_flutter"
    "modules/new_serverpod_auth/serverpod_auth_bridge/serverpod_auth_bridge_client"

    "modules/legacy/serverpod_chat/serverpod_chat_server"
    "modules/legacy/serverpod_chat/serverpod_chat_client"
    "modules/legacy/serverpod_chat/serverpod_chat_flutter"

    "templates/serverpod_templates/projectname_flutter"
    "templates/serverpod_templates/projectname_client"
    "templates/serverpod_templates/projectname_server"
    "templates/serverpod_templates/modulename_client"
    "templates/serverpod_templates/modulename_server"

    "integrations/serverpod_cloud_storage_s3"
    "integrations/serverpod_cloud_storage_gcp"

    "tools/serverpod_cli"
)

exit_code=0

echo "### Running dart analyze with log level '$LOG_LEVEL'"
for i in "${projectPaths[@]}"
do
   dart analyze $LOG_LEVEL $i

   if [ $? != 0 ]; then
     exit_code=1
   fi
done

exit $exit_code
